//
//  YDWelcomeController.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/11.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
import SDWebImage

class YDWelcomeController: UIViewController {
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
//  更新图片的约束
        headImgView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        
//        设置动画效果
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //强制刷新UI(如果你使用的是约束 就需要这么干)
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.headImgView.alpha = 1
                    }, completion: { (_) -> Void in
//                        发送通知切换跟控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(WEBSWITCHROOTVCNOTIFACATION, object: nil)
                        
                })
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
//    设置视图
    func setUpUI(){
    
//    添加view
        view.addSubview(bgImgView)
        view.addSubview(headImgView)
        view.addSubview(messageLabel)
//        添加约束
        bgImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
            
        }
         headImgView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(headImgView.snp_bottom).offset(16)
        }
    }
    
//    懒加载控件
//    背景图片
    private lazy var bgImgView:UIImageView = UIImageView(imageName: "ad_background")
    
//    头像
    private lazy var headImgView:UIImageView = {
    let img = UIImageView(imageName: "avatar_default_big")
        
        
//        设置为圆
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        img.yd_setImageWithString(YDUserAccountViewModel.sharedTools.useAccount?.avatar_large ?? "", placeholderImgName: "avatar_default_big")
        
//        img.sd_setImageWithURL(NSURL(string: "YDUserAccountViewModel.sharedTools.useAccount?.avatar_large" ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
        
        return img
    
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(18)
        lab.text = "欢迎回来"
        lab.textColor = UIColor.darkGrayColor()
        lab.textAlignment = .Center
        lab.alpha = 0
        return lab
    }()

}
